package cn.silver.module.authority.domain;

import cn.silver.framework.core.utils.StringUtils;
import cn.silver.framework.mybatis.annotation.Table;
import cn.silver.framework.mybatis.annotation.TableField;
import cn.silver.framework.mybatis.enums.SearchEnums;
import cn.silver.framework.mybatis.domain.BusEntity;
import cn.silver.framework.mybatis.enums.RoleTypeEnums;
import cn.silver.framework.core.enums.SystemStatusEnums;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * 角色表 sys_role
 *
 * @author Silver Zhou
 */

@Data
@NoArgsConstructor
@Table(name = "sys_role")
@EqualsAndHashCode(callSuper = true)
@Schema(name = "SysRole", title = "角色信息对象")
public class SysRole extends BusEntity {

    /**
     * 角色编码
     */
    @Schema(title = "角色编码")
    @TableField(name = "code", searchType = SearchEnums.LIKE)
    private String code;
    /**
     * 角色名称
     */
    @Schema(title = "角色名称")
    @TableField(name = "name", searchType = SearchEnums.LIKE)
    private String name;
    /**
     * 角色类型
     */
    @Schema(title = "角色类型")
    @TableField(name = "type", searchType = SearchEnums.EQ)
    private String type;
    /**
     * 数据范围
     */
    @Schema(title = "数据范围")
    @TableField(name = "scope", searchType = SearchEnums.EQ)
    private String scope;
    /**
     * 管理对象
     */
    @Schema(title = "管理对象")
    @TableField(name = "bus_id", searchType = SearchEnums.EQ)
    private String busId;
    /**
     * 角色状态
     */
    @Schema(title = "角色状态")
    @TableField(name = "status", searchType = SearchEnums.EQ)
    private String status;

    @Schema(title = "是否初始化角色")
    private Boolean built;

    @Schema(title = "角色状态")
    private Integer sort;

    public SysRole(String code, String name, String type, String scope, Boolean built) {
        this.code = code;
        this.name = name;
        this.type = type;
        this.scope = scope;
        this.built = built;
    }

    public static SysRole getInstance(RoleTypeEnums enums) {
        return new SysRole(enums.getCode(), enums.getName(), enums.getCode(), enums.getScope(), enums.getBuilt());
    }

    @Override
    public void preInsert() {
        super.preInsert();
        if (this.built == null) {
            this.built = false;
        }
        if (StringUtils.isBlank(this.status)) {
            this.status = SystemStatusEnums.NORMAL.getCode();
        }
    }

    @Override
    public void preUpdate() {
        super.preUpdate();
        if (this.built == null) {
            this.built = false;
        }
        if (StringUtils.isBlank(this.status)) {
            this.status = SystemStatusEnums.NORMAL.getCode();
        }
    }
}
